﻿using System;
using System.Collections.Generic;
using System.IO;
using LitJson;
using QuickConvert;
using QuickConvert.Json;

namespace ConsoleSample
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("This is the sample for QuickConvert");

            SingleItemConvertStructure structure = new SingleItemConvertStructure()
            {
                AssemblyName = "ConsoleSample",
                ConvertFuncType = "ConsoleSample.TestClass",
                ConvertFuncName = "TestStaticFunc",
                IsStaticClass = false
            };

            var res = JsonToJsonConfig.ConvertTo(structure, "a");
            Console.WriteLine(res);

            string oriStr = File.ReadAllText(@"C:\OTHERS\CODES\QuickConvert\JsonSample\ori.json");
            string convertStr = File.ReadAllText(@"C:\OTHERS\CODES\QuickConvert\JsonSample\J2JMainConfig.json");
            
            Converter thisConverter = new Converter();

            res = thisConverter.JsonToJson(new JsonToJsonConfig()
            {
                OriStructure = oriStr,
                ConvertStructure = convertStr,
            });

            Console.WriteLine(res);
        }
    }


    public class TestClass
    {
        public static string TestStaticFunc(string a)
        {
            Console.WriteLine("Invoke: " + a);
            return "123 " + a + " 123";
        }
    }

    /// <summary>
    /// ConvertSample是知道原始Json细节的
    /// </summary>
    public class ConverterSample
    {
        public static string GetProjectFromOriJson(string oriJson)
        {
            JsonData data = JsonMapper.ToObject(oriJson);
            var str = data["name"].ToString();
            return str;
        }

        public string GetMergedDataList(string oriJson)
        {
            JsonData data = JsonMapper.ToObject(oriJson);

            JsonData resData = new JsonData();
            
            JsonData addData = data["address"];
            JsonData linksData = data["links"];

            Converter thisConverter = new Converter();
            string convertStr = File.ReadAllText(@"C:\OTHERS\CODES\QuickConvert\JsonSample\J2JMergeDataConfig.json");
            foreach (JsonData singleLinkData in linksData)
            {
                JsonData thisLinkRelated = new JsonData();
                thisLinkRelated["Link"] = singleLinkData;
                thisLinkRelated["Address"] = addData;

                string aftStr = thisConverter.JsonToJson(
                    new JsonToJsonConfig()
                    {
                        OriStructure = thisLinkRelated.ToJson(),
                        ConvertStructure = convertStr,
                    }
                );
                JsonData thisData = JsonMapper.ToObject(aftStr);

                resData.Add(thisData);
            }

            return resData.ToJson();
        }

        public string GetSingleMergeDataName(string oriJson)
        {
            JsonData oriData = JsonMapper.ToObject(oriJson);

            JsonData addData = oriData["Link"];
            return addData["url"].ToString();
        }

        public string GetSingleMergeDataURL(string oriJson)
        {
            JsonData oriData = JsonMapper.ToObject(oriJson);

            JsonData addData = oriData["Link"];
            return addData["name"].ToString();
        }

        public string GetSingleMergeDataCITY(string oriJson)
        {
            JsonData oriData = JsonMapper.ToObject(oriJson);

            JsonData addData = oriData["Address"];
            return addData.ToJson();
        }
    }
}
